package com.lovo.sgproj.frame;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;

import com.lovo.netCRM.component.LovoTree;
import com.lovo.netCRM.component.LovoTreeNode;
import com.lovo.sgproj.Config;
import com.lovo.sgproj.bean.ClassBean;
import com.lovo.sgproj.bean.RoomBean;
import com.lovo.sgproj.bean.StudentBean;
import com.lovo.sgproj.frame.classmanage.ClassManagePanel;
import com.lovo.sgproj.frame.roommanage.RoomManagePanel;
import com.lovo.sgproj.frame.studentmanage.StudentManagePanel;
import com.lovo.sgproj.service.ClassService;
import com.lovo.sgproj.service.RoomService;
import com.lovo.sgproj.service.StudentService;
import com.lovo.sgproj.service.imp.ClassServiceImp;
import com.lovo.sgproj.service.imp.RoomServiceImp;
import com.lovo.sgproj.service.imp.StudentServiceImp;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainFrame extends JFrame {

    ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);

    private Container contentP;

    private LovoTree tree;

    private LovoTreeNode rootNode;//根节点
    private LovoTreeNode resourceMagNode;//资料管理
    private LovoTreeNode studentNode;
    private LovoTreeNode classNode;
    private LovoTreeNode roomMagNode;
    private LovoTreeNode roomNode;


    private JPanel cardPanel;
    private WelcomePanel wp;
    private StudentManagePanel smP;
    private ClassManagePanel cmP;
    private RoomManagePanel rmP;

    public StudentManagePanel getSmP() {
        return smP;
    }

    public void setSmP(StudentManagePanel smP) {
        this.smP = smP;
    }

    public RoomManagePanel getRmP() {
        return rmP;
    }

    public void setRmP(RoomManagePanel rmP) {
        this.rmP = rmP;
    }

    public MainFrame() {
        Toolkit tk = Toolkit.getDefaultToolkit();
        this.setIconImage(tk.createImage("image/hp.JPG"));
        this.setTitle("宿舍管理系统");
        this.setLocation(140, 190);
        this.setSize(1000, 580);
        this.setResizable(false);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.initTree();
        this.initCard();

        this.setVisible(true);
    }

    public LovoTreeNode getStudentNode() {
        return studentNode;
    }

    public void setStudentNode(LovoTreeNode studentNode) {
        this.studentNode = studentNode;
    }

    public LovoTreeNode getRoomNode() {
        return roomNode;
    }

    public void setRoomNode(LovoTreeNode roomNode) {
        this.roomNode = roomNode;
    }

    public LovoTreeNode getClassNode() {
        return classNode;
    }

    public void setClassNode(LovoTreeNode classNode) {
        this.classNode = classNode;
    }

    private void initTree() {
        // TODO Auto-generated method stub
        this.contentP = this.getContentPane();
//		this.contentP.setBackground(Color.WHITE);
        this.contentP.setLayout(null);


        this.rootNode = new LovoTreeNode("宿管系统管理");
        this.resourceMagNode = new LovoTreeNode("资料管理");
        this.studentNode = new LovoTreeNode("学生管理") {
            @Override
            public void addListener() {
                // TODO Auto-generated method stub
                // 向后台请求获取StudentList
                StudentService stuService = context.getBean(StudentServiceImp.class);
                List<StudentBean> stuLst = stuService.showAllStudent();
                //然后调用smp的addStudentsToTab方法
                smP.addStudentsToTab(stuLst.subList(0, 2));

                CardLayout cl = (CardLayout) MainFrame.this.cardPanel.getLayout();
                cl.show(MainFrame.this.cardPanel, "student");
            }
        };
        this.classNode = new LovoTreeNode("班级管理") {
            @Override
            public void addListener() {
                // TODO Auto-generated method stub
                ClassService classService = context.getBean(ClassServiceImp.class);
                ArrayList<ClassBean> classLst = classService.getAllClass();
                cmP.addClassToTab(classLst);

                CardLayout cl = (CardLayout) MainFrame.this.cardPanel.getLayout();
                cl.show(MainFrame.this.cardPanel, "class");
            }
        };
        this.roomMagNode = new LovoTreeNode("房间管理");
        this.roomNode = new LovoTreeNode("房间信息") {
            @Override
            public void addListener() {
                // TODO Auto-generated method stub
                RoomService roomService = context.getBean(RoomServiceImp.class);
                ArrayList<RoomBean> roomLst = roomService.showAllRoom();
                rmP.addRoomToTab(roomLst);

                CardLayout cl = (CardLayout) MainFrame.this.cardPanel.getLayout();
                cl.show(MainFrame.this.cardPanel, "room");
            }
        };

        //第一层
        this.rootNode.add(this.resourceMagNode);
        this.rootNode.add(this.roomMagNode);

        //第二层
        this.resourceMagNode.add(this.studentNode);
        this.resourceMagNode.add(this.classNode);

        this.roomMagNode.add(this.roomNode);


        this.tree = new LovoTree(this.rootNode);
        this.tree.setFont(new Font("微软雅黑", Font.ITALIC, 20));
        this.tree.setBounds(15, 20, 150, 505);
        this.contentP.add(this.tree);
    }

    private void initCard() {
        // TODO Auto-generated method stub
        this.cardPanel = new JPanel();
        this.cardPanel.setLayout(new CardLayout());

        this.wp = new WelcomePanel();
        this.smP = new StudentManagePanel(this);
        this.cmP = new ClassManagePanel(this);
        this.rmP = new RoomManagePanel(this);

        this.cardPanel.add(this.wp, "welcome");
        this.cardPanel.add(this.smP, "student");
        this.cardPanel.add(this.cmP, "class");
        this.cardPanel.add(this.rmP, "room");


        this.cardPanel.setBounds(170, 20, 800, 505);
        this.contentP.add(this.cardPanel);
    }
}
